package org.jeecg.modules.wlhy.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressReceive;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressSend;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressReceiveService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressSendService;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: hy_customer_address_send发货地址管理
 * @Author: jeecg-boot
 * @Date:   2020-12-02
 * @Version: V1.0
 */
@Api(tags="hy_customer_address_send")
@RestController
@RequestMapping("/customer/hyCustomerAddressSend")
@Slf4j
public class HyCustomerAddressSendController extends JeecgController<HyCustomerAddressSend, IHyCustomerAddressSendService> {
	@Autowired
	private IHyCustomerAddressSendService hyCustomerAddressSendService;

	@Autowired private IHyCustomerAddressReceiveService hyCustomerAddressReceiveService;

	 @Autowired
	 private UserUtil userUtil;

	 @Autowired
	 private IHyProjectService hyProjectService;



	@ApiOperation(value="hy_customer_address_send-分页列表查询", notes="hy_customer_address_send-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerAddressSend hyCustomerAddressSend,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		QueryWrapper<HyCustomerAddressSend> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(hyCustomerAddressSend.getAddressName())){
			queryWrapper.like("address_name",hyCustomerAddressSend.getAddressName().trim());
			hyCustomerAddressSend.setAddressName("");
		}

		HyCustomer customer = userUtil.getCustomer();
		if(customer!=null){
			queryWrapper.eq("customer_id",customer.getId());
		}




		QueryGenerator.initQueryWrapper(queryWrapper , hyCustomerAddressSend, req.getParameterMap());
		Page<HyCustomerAddressSend> page = new Page<HyCustomerAddressSend>(pageNo, pageSize);
		IPage<HyCustomerAddressSend> pageList = hyCustomerAddressSendService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyCustomerAddressSend
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_send-添加")
	@ApiOperation(value="hy_customer_address_send-添加", notes="hy_customer_address_send-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("customer:sendAddr:add")
	public Result<?> add(@RequestBody HyCustomerAddressSend hyCustomerAddressSend) {
		if("1".equals(hyCustomerAddressSend.getIzToCommon())){
			if(StringUtils.isEmpty(hyCustomerAddressSend.getCustomerId())){
				return Result.error("customerId不能为空");
			}
		}else {
			if (userUtil.isSys()) {
				if (StringUtils.isEmpty(hyCustomerAddressSend.getCustomerId())) {
					return Result.error("customerId不能为空");
				}
			} else {
				HyCustomer customer = userUtil.getCustomer();
				if (customer == null) {
					return Result.error("货主信息不存在");
				}
				hyCustomerAddressSend.setCustomerId(customer.getId());
			}

			if ("1".equals(hyCustomerAddressSend.getIstb())) {
				HyCustomerAddressReceive hyCustomerAddressReceive = new HyCustomerAddressReceive();
				BeanUtils.copyProperties(hyCustomerAddressSend, hyCustomerAddressReceive);
				hyCustomerAddressSend.setAreaCode(hyCustomerAddressSend.getAreaCode().substring(0,6));
				hyCustomerAddressReceiveService.save(hyCustomerAddressReceive);
			}
		}
		hyCustomerAddressSend.setAreaCode(hyCustomerAddressSend.getAreaCode().substring(0,6));
		hyCustomerAddressSendService.save(hyCustomerAddressSend);
		return Result.OK("添加成功！" , hyCustomerAddressSend);
	}

	/**
	 *  编辑
	 *
	 * @param hyCustomerAddressSend
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_send-编辑")
	@ApiOperation(value="hy_customer_address_send-编辑", notes="hy_customer_address_send-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("customer:sendAddr:edit")
	@CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE ,key = "'SimpleKey [hy_customer_address_send,address_name,id,' + #hyCustomerAddressSend.id + ']'")
	public Result<?> edit(@RequestBody HyCustomerAddressSend hyCustomerAddressSend) {
		HyCustomer customer = userUtil.getCustomer();
		if(customer!=null){
			if(!StringUtils.equals(hyCustomerAddressSend.getCustomerId(),customer.getId())){
				return Result.error("数据不存在!");
			}
		}
		hyCustomerAddressSend.setAreaCode(hyCustomerAddressSend.getAreaCode().substring(0,6));
		hyCustomerAddressSendService.updateById(hyCustomerAddressSend);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_send-通过id删除")
	@ApiOperation(value="hy_customer_address_send-通过id删除", notes="hy_customer_address_send-通过id删除")
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("customer:sendAddr:delete")
	@CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE ,key = "'SimpleKey [hy_customer_address_send,address_name,id,' + #id + ']'")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		HyCustomer customer = userUtil.getCustomer();
		HyCustomerAddressSend addressSend = hyCustomerAddressSendService.getById(id);
		if(addressSend == null){
			return Result.error("数据不存在!");
		}

		if(customer!=null){
			if(!StringUtils.equals(addressSend.getCustomerId(),customer.getId())){
				return Result.error("数据不存在!");
			}
		}
		hyCustomerAddressSendService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_send-批量删除")
	@ApiOperation(value="hy_customer_address_send-批量删除", notes="hy_customer_address_send-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("customer:sendAddr:deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCustomerAddressSendService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_customer_address_send-通过id查询", notes="hy_customer_address_send-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerAddressSend hyCustomerAddressSend = hyCustomerAddressSendService.getById(id);
		if(hyCustomerAddressSend==null) {
			return Result.error("未找到对应数据");
		}
//		HyCustomer customer = userUtil.getCustomer();
//		if(customer!=null){
//			if(!StringUtils.equals(hyCustomerAddressSend.getCustomerId(),customer.getId())){
//				return Result.error("数据不存在!");
//			}
//		}
		return Result.OK(hyCustomerAddressSend);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCustomerAddressSend
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("customer:sendAddr:exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HyCustomerAddressSend hyCustomerAddressSend) {
        return super.exportXls(request, hyCustomerAddressSend, HyCustomerAddressSend.class, "hy_customer_address_send");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@RequiresPermissions("customer:sendAddr:importExcel")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCustomerAddressSend.class);
    }

}
